<?php
require_once 'lib/utils/CXmlCategory.php';
class CRepairData
{
	public $creteria;
	function __construct($creteria)
	{
		$this->creteria = $creteria;
	}
	
	function repair(){
		switch ($this->creteria){
			case 'product': return $this->repair_product();
			case 'category_path': return $this->syn_category_path();
			case 'product_properties': return $this->edit_prd_properties();
			case 'product_cache_properties': return $this->edit_cache_properties();
			case 'product_cache_all_properties': return $this->edit_cache_all_properties();
			default: break;
		}
	}
	
	function repair_product(){
		$all_products = CTable('product')->select_all(array('portal_id'=>portal()->id));
		if(!empty($all_products)){
			CTable('product')->update(array('substitute_id'=>0), array('portal_id'=>portal()->id));
			foreach ($all_products as $product){
				if($product['market_price'] && $product['price']){
					$data['save_price'] = $product['market_price']-$product['price'];
					$data['save_percent'] = round($data['save_price']/$product['market_price'],3)*100;
					CTable('product')->update($data, array('id'=>$product['id']));
					CProduct::update_substitute($product['id']);
				}
			}
			echo "Đã cập nhật thành công";
		}else {
			echo "Portal này không có sản phẩm nào";
		}
	}
	
	function syn_category_path(){
		//$file_name = DATA_DIR.portal()->id.'/product_categories.xml';
		$all_products = CTable('product')->select_all(array('portal_id'=>portal()->id));
		if(!empty($all_products)){
			foreach ($all_products as $product){
				$cat_path = '';
				if(!empty($product['category_id'])){
					CStoreCategoryList::getInstance()->getCatePath($product['category_id'], $cat_path);
					//CXmlCategory($file_name)->get_cat_path($cat_path, 'cats//cat',$product['category_id']);
					//echo CStoreCategoryList::getInstance()->getOneRowsCate($product['category_id'], 'code').'<br />';
					$category = CStoreCategoryList::getInstance()->getOneRowsCate($product['category_id'], 'code');
					$data['category_path'] = $cat_path;
					//$data['cate_id'] = $category;
					CTable('product')->update($data, array('id'=>$product['id']));
				}
			}
			echo "Đã cập nhật thành công";
		}else {
			echo "Portal này không có sản phẩm nào";
		}
	}
	
	function edit_prd_properties(){
		$all_products = CTable('product')->select_all(array('portal_id'=>portal()->id));
		
		if(!empty($all_products)){
			$all_categories = array();
			//$category_store = new CDataXmlStore(DATA_DIR.portal()->id.'/product_categories.xml','/root/');
			foreach ($all_products as $product){
				$file_name = CStoreCategoryList::getInstance()->getFilePropertyCategory($product['category_id']);
				$parent_cat = $product['category_id'];
				if(!isset($all_categories[$parent_cat])){
					$groups = CDataXmlStore($file_name)->select_all(false);
					$all_categories[$parent_cat] = array();
					if(isset($groups['groups'])){
						$all_categories[$parent_cat] = $groups['groups'];
					}
				}

				//Getting file_name
				$groups = $all_categories[$parent_cat];
				$properties = CTable(CStore($product['properties']))->select_all();
				// Properties Treatment
				if(count($groups) > 0){
					foreach ($groups as $group){
						if(isset($group['fields']) and is_array($group['fields']))
						{
							foreach($group['fields'] as $field)
							{
								if($field['type'] == 'SELECT'){
									$val = '';
									if(isset($properties[$group['id']][$field['id']])){
										$val = $properties[$group['id']][$field['id']];
									}
									if(isset($field['values']) and $val){
										foreach ($field['values'] as $one){
											if(array_search($val, $one)){
												$properties[$group['id']][$field['id']] = $one['id'];
											}
										}
									}
								}
							}
						}
					}
				}
				$data['properties'] = var_export($properties, true);
				if($data['properties'] != 'array ( )'){
					CTable('product')->update($data, array('id'=>$product['id']));
				}				
			}
			echo "Đã cập nhật thành công";
		}else {
			echo "Portal này không có sản phẩm nào";
		}
	}
	
	function edit_cache_properties(){
		$all_products = CTable('product')->select_all(array('portal_id'=>portal()->id));
		if(!empty($all_products)){
			$categories = array();
			//$category_store = new CDataXmlStore(DATA_DIR.portal()->id.'/product_categories.xml','/root/');
			foreach ($all_products as $product){
				$category = $product['category_id'];
				if(!isset($categories[$category]))
				{
					$file_name = CStoreCategoryList::getInstance()->getFilePropertyCategory($product['category_id']);
					$all_groups = CTable("groups/group")->change_store(new CDataXmlStore($file_name))->select_all();
					$groups = array();
					foreach($all_groups as $group)
					{
						$fields = array();
						if(isset($group['fields']) and is_array($group['fields']))
						{
							foreach($group['fields'] as $field)
							{
								if(isset($field['show_tooltip']) and $field['show_tooltip'])
								{
									if(isset($field['values']) and is_array($field['values']))
									{
										$values = array();
										foreach($field['values'] as $value)
										{
											$values[$value['id']] = $value['label'];
										}
										$field['values'] = $values;
									}
									$fields[$field['id']] = $field;
								}
							}
						}
						$group['fields'] = $fields;
						$groups[$group['id']] = $group;
					}
					
					$categories[$category] = $groups;
				}
				$properties = CTable(CStore($product['properties']))->select_all();
				$output = '' ;
				if(!empty($properties))
				{
					foreach($properties as $group_id=>$group)
					{
						foreach($group as $key=>$value)
						{
							if(isset($categories[$category]) and isset($categories[$category][$group_id]) and isset($categories[$category][$group_id]['fields']) and isset($categories[$category][$group_id]['fields'][$key]))
							{
								$field_name = $categories[$category][$group_id]['fields'][$key]['label'];
								if(isset($categories[$category][$group_id]['fields'][$key]['type']) and $categories[$category][$group_id]['fields'][$key]['type'] == 'SELECT')
								{
									if(isset($categories[$category][$group_id]['fields'][$key]['values']) and isset($categories[$category][$group_id]['fields'][$key]['values'][$value]) and $categories[$category][$group_id]['fields'][$key]['values'][$value])
									{
										$value = $categories[$category][$group_id]['fields'][$key]['values'][$value];
										if($value == 'None')
										{
											$value = '';
										}
									}
								}
								$output .= '<strong>'.$field_name.'</strong>:&nbsp;'.$value.'<br>';
							}
						}
					}
				}
				if(!empty($output))
				{
					$data['cache_properties'] = $output;
					CTable('product')->update($data, array('id'=>$product['id']));
				}
			}
			echo "Đã cập nhật thành công";
		}else {
			echo "Portal này không có sản phẩm nào";
		}
	}
	
	function edit_cache_all_properties(){
		$all_products = CTable('product')->select_all(array('portal_id'=>portal()->id));
		if(!empty($all_products)){
			$all_categories = array();
			//$category_store = new CDataXmlStore(DATA_DIR.portal()->id.'/product_categories.xml','/root/');
			foreach ($all_products as $product){
				$file_name = CStoreCategoryList::getInstance()->getFilePropertyCategory($product['category_id'], 'data/default/product_categories/');
				$properties = CTable(CStore($product['properties']))->select_all();
				$parent_cat = $product['category_id'];
				if(!isset($all_categories[$parent_cat]))
				{
					$store = new CDataXmlStore($file_name,'/root/');
					$all_categories[$parent_cat] = CTable("groups/group")->change_store($store)->select_all();
				}
				$groups = $all_categories[$parent_cat];
				$properties_tags = '';
				foreach($groups as $group_id=>&$group)
				{
					if(isset($group['fields']) and is_array($group['fields']))
					foreach($group['fields'] as $field_id => &$field)
					{
						if(isset($field['hide_detail']) and $field['hide_detail'])
						{
							if(isset($properties[$group['id']]) and isset($properties[$group['id']][$field['id']]))
							{
								$properties_tags .= (($properties[$group['id']][$field['id']] == 'Có')?$field['label'].' : ':'').$properties[$group['id']][$field['id']].';';
							}
							unset($group['fields'][$field_id]);
						}
						else
						if(isset($properties[$group['id']]) and isset($properties[$group['id']][$field['id']]))
						{
							$field['value'] = $properties[$group['id']][$field['id']];
							if($field['type'] == 'SELECT' and isset($field['values']))
							{
								foreach($field['values'] as $value)
								{
									if($value['id'] == $field['value'])
									{
										if($value['label'] == 'None')
										{
											$field['value'] = '';
										}
										else
										{
											$field['value'] = $value['label'];
										}
										break;
									}
								}
							}
							$properties_tags .= (($field['value']=='Có')?$field['label'].' : ':'').$field['value'].'; ';
						}
						else
						{
							$field['value'] = '';
						}
					}
					if(!isset($group['fields']) or !$group['fields'])
					{
						unset($groups[$group_id]);
					}
				}
				$aPropertyTags = array();
				if(!empty($properties_tags)){
					$aPropertyTags = array('properties_tags' => $properties_tags);
				}
				CTable('product')->update(array(
					'cache_all_properties' => output()->get(CForm('lib/utils/specifications.tpl')->parse(array('groups'=>$groups))),					
				)+$aPropertyTags, array('id'=>$product['id']));
			}
			echo "Đã cập nhật thành công";
		}else {
			echo "Portal này không có sản phẩm nào";
		}
	}
	
}
function CRepairData($creteria){
	return new CRepairData($creteria);
}
?>